package com.sxygsj.android.personnelscreen.main;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.sxygsj.android.personnelscreen.R;

import java.util.List;

public class UserInfoListAdapter extends ListAdapter<UserInfo, UserInfoListAdapter.ViewHolder> {

    public static final DiffUtil.ItemCallback<UserInfo> DIFF_CALLBACK =
            new DiffUtil.ItemCallback<UserInfo>() {
                @Override
                public boolean areItemsTheSame(
                        @NonNull UserInfo oldUser, @NonNull UserInfo newUser) {
                    return oldUser.getCardNumber().equals(newUser.getCardNumber());
                }

                @Override
                public boolean areContentsTheSame(
                        @NonNull UserInfo oldUser, @NonNull UserInfo newUser) {
                    return oldUser.equals(newUser);
                }

                @Nullable
                @Override
                public Object getChangePayload(@NonNull UserInfo oldItem, @NonNull UserInfo newItem) {
                    Bundle payload = null;
                    if (!areContentsTheSame(oldItem, newItem)) {
                        payload = new Bundle();
                        payload.putParcelable("user", newItem);
                    }
                    return payload;
                }

            };

    public UserInfoListAdapter() {
        super(DIFF_CALLBACK);

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user_info_list_recycler, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        UserInfo item = getItem(position);
        if (item != null) {
            holder.main_tv_info.setText(
                    "卡号：" + item.getCardNumber() + "\n" +
                            "姓名：" + item.getPersonName() + "\n" +
                            "部门：" + item.getDepartment());
            Glide.with(holder.itemView).load(item.getImageUrl()).into(holder.main_iv_headPortrait);

        }
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull List<Object> payloads) {
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        } else {

            Bundle payload = (Bundle) payloads.get(0);
            UserInfo item = payload.getParcelable("user");

            holder.main_tv_info.setText(
                    "卡号：" + item.getCardNumber() + "\n" +
                            "姓名：" + item.getPersonName() + "\n" +
                            "部门：" + item.getDepartment());
            Glide.with(holder.itemView).load(item.getImageUrl()).into(holder.main_iv_headPortrait);
        }

    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView main_tv_info;
        ImageView main_iv_headPortrait;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            main_tv_info = itemView.findViewById(R.id.main_tv_info);
            main_iv_headPortrait = itemView.findViewById(R.id.main_iv_headPortrait);
        }
    }

}

